/**
 * Created by lijingwen on 2015-02-07.
 */

$(function(){
    var listdata;
    $('#refreshlist').bind('click',getlist);
    getlist();
    $('#addnickname').unbind('click').live('click',addnickname);
    $('#addcontentbotton').click(addnewcontentbutton);
    $('.tag').live('click',function(){
        var tag = $(this);
        $(tag).fadeOut('fast');
    });
    $('.deletecontent').live('click',function(){
        var contentname = $(this).attr('title');
        deletecontent(contentname);
    });
    $('.traversebutton').live('click',function(){
        var contentname = $(this).attr('title');
        traversecontent(contentname);
    });

    function addnickname(){
        var newnickname = $('#newcontentnickname');
        var newcontentname = $('#newcontentname');
        var nicknamecontainer =  $('.nicknamecontainer');
        var repeat=0;
        for(var i=0;i< $('.nicknamecontainer div').length;i++){
            console.log('len='+$('.nicknamecontainer div').length);
            if(newnickname.val() == $('.nicknamecontainer div').eq(i).children('span').text()){
                repeat=1;
            }
        }
        if(newcontentname.val() == null || newcontentname.val()==''){
            alert('节目名称不能为空。');
            newcontentname.focus();
        }
        else if(newnickname.val() == null || newnickname.val() == '' ){
            alert('别名不能为空。');
            newnickname.focus();
        }
        else if(repeat==1){
            console.log(repeat);
            alert('别名 "'+newnickname.val()+'" 已经存在。');
            newnickname.focus();
        }
        else{
            var tag = '<div class="tag"><span>'+newnickname.val()+'</span>x</div>';
            nicknamecontainer.append(tag);
            newnickname.val('');
            newnickname.focus();
        }

    }

    function addnewcontentbutton(){
        //verify
        var newcontentname = $('#newcontentname');
        var newcontentid =  $('#newcontentid');
        if(newcontentid == 0){
            alert('连接服务器失败，无法处理提交请求');
        }
        if(newcontentname.val() == null || newcontentname.val()==''){
            alert('节目名称不能为空。');
            newcontentname.focus();
        }
        else{
            var content ={
                'contentname':'',
                'nickname':[]
            };
            content.contentid = newcontentid.text();
            content.contentname = newcontentname.val();
            for(var i=0;i<$('.nicknamecontainer div').length;i++){
                content.nickname.push($('.nicknamecontainer div').eq(i).children('span').text());
            }
            console.log(content);

            load_data("/AddNewContent",content,null,successinsertcontent,defaulterror);
        }

    }

    function successinsertcontent(data){
        var check = data;
        if(check.check && check.flag){
            alert('节目更新成功!');
            getlist();
        }
        else if(check.check && check.flag==0){
            alert('插入成功!');
        }
        else{
            alert('操作失败！');
        }


    }

    function getlist(){
        var newnickname = $('#newcontentnickname');
        var newcontentname = $('#newcontentname');
        var nicknamecontainer =  $('.nicknamecontainer');
        newnickname.val('');
        newcontentname.val('').focus();
        nicknamecontainer.empty();
        load_data("/ShowAllContents",null,null,showcontentlist,failshowcontentlist);
    }

    //show list
    function showcontentlist(data){
        console.log(data);
        listdata = data;
        var html ='';
        for(var i=0;i<data.length;i++){
            var contentid = data[i]._id;
            html += '<tr>'+
                '<td class="contentid">'+(i+1)+'</td>'+
                '<td class="contentname"><a href="#">'+data[i].contentname+'</a></td>'+
                '<td class="contentnickname">'+data[i].nickname+'</td>'+
                '<td class="contentlist">'+data[i].list.length+'</td>'+
                '<td class="contentopp">'+
                ' <a title="'+data[i].contentname+'" class="button minbutton greenbutton traversebutton">遍历</a>'+
                '<a id="'+data[i]._id+'" title="'+data[i].contentname+'" class="button minbutton redbutton deletecontent">删除</a>'+
                '</td>'+'</tr>';
        }
        $('.content-tbody').html(html);
    }
    function failshowcontentlist(){
        alert("列表加载失败。");
    }

    function deletecontent(contentname){
        var name ={'contentname':contentname};
        load_data("/DeleteContent",name,null,successdelete,defaulterror);
    }
    function successdelete(data){
        console.log(data);
        if(data){
            alert('删除成功。');
        }
    }

    function traversecontent(contentname){
        var name ={'contentname':contentname};
        load_data("/TraverseContent",name,null,successtraverse,defaulterror);
    }
    function successtraverse(data){
        console.log(data);
        alert('遍历完成');
    }



});